package com.fruit.system.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import com.fruit.util.PagerContext;


public class PagerFilter implements Filter
{

	@Override
	public void destroy()
	{

	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException
	{
		int currentPage = 1;
		int pageSize=15;
		try
		{
			currentPage=Integer.parseInt(req.getParameter("currentPage"));
			pageSize=Integer.parseInt(req.getParameter("pageSize"));
		}
		catch (Exception e) {
			//e.printStackTrace();
		}
		try{
			PagerContext.setCurrentPage(currentPage);
			PagerContext.setPageSize(pageSize);
			PagerContext.setOrder(req.getParameter("order"));
			PagerContext.setSort(req.getParameter("sort"));
			chain.doFilter(req, resp);
		}
		finally{
			PagerContext.removeOrder();
			PagerContext.removeCurrentPage();
			PagerContext.removePageSize();
			PagerContext.removeSort();
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException
	{

	}

}
